//
//  readfileTool.m
//  点逸通讯录
//
//  Created by tang guifu  on 12-6-5.
//  Copyright 2012年 CZDYTECH . All rights reserved.
//  Description: The shared interface is in viewDidAppear,
//               if you want to share file please implement this method firstly,
//               then you will see the file appear in the shared documentdesk.

#import "readfileTool.h"
#import "RequestServer.h"
#import "BSAppDelegate.h"
@implementation readfileTool
@synthesize ispersonal;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithUrl:(NSString *)url
{
    self = [super init];
    if (self) {
        requestStr = url;
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBarHidden =NO;
//    self.toolBar = [[UIView alloc] initWithFrame:CGRectMake(0, 40, self.view.width, 40)];
//    self.toolBar.backgroundColor = [UIColor colorWithHexString:@"00b5ed"];
    
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setFrame:CGRectMake(12, 6, 28, 28)];
    [backButton setImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
    backButton.backgroundColor = [UIColor clearColor];
    [backButton addTarget:self action:@selector(Goback:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
    
    [self.webView setFrame:CGRectMake(0, 0, self.view.width, self.view.height-0)];
    NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
    NSString *str_urlCurrent =[defaults objectForKey:@"current_ServerUrl"];
    NSString *str = @"";
    if ([requestStr hasPrefix:@"http://"]) {
        str = [NSString stringWithFormat:@"%@",requestStr];
    }else{
        str = [NSString stringWithFormat:@"%@%@",str_urlCurrent, requestStr];
    }
    
    NSFileManager *manager = [NSFileManager defaultManager];
	NSString* configCacheFilePath = [CatchPath(@"fefwea") stringByAppendingFormat:@"/%@", str];
    NSURL *url ;
	if (![manager fileExistsAtPath:configCacheFilePath]) {
        url = [[NSURL alloc]initWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
	} else {
        url = [NSURL fileURLWithPath:[configCacheFilePath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    }
    
    if ([str hasSuffix:@".txt"]) {
        NSData *data =[NSData dataWithContentsOfURL:url];
        [self.webView loadData:data MIMEType:@"text/html" textEncodingName:@"GBK" baseURL:[NSURL URLWithString:@"http://iphoneincubator.com"]];
    }else if([str hasSuffix:@".mov"]||[str hasSuffix:@".mp4"]){
        NSURL *movieURL = [NSURL fileURLWithPath:requestStr];
        MPMoviePlayerController *playerViewController =
        [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
        playerViewController.scalingMode = MPMovieScalingModeAspectFit;
        [self.view addSubview:playerViewController.view];
    }else
    {
        NSURLRequest *req =[[NSURLRequest alloc]initWithURL:url];
        [self startWaitCursor];
        self.webView.scalesPageToFit = YES;
        self.webView.delegate =self;
        [self.webView loadRequest:req];
    }
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self stopWaitCursor];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self stopWaitCursor];
    UIAlertView *alter =[[UIAlertView alloc]initWithTitle:@"" message:@"该文件不存在!" delegate:self cancelButtonTitle:@"返回" otherButtonTitles: nil];
    [alter show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (0 == buttonIndex) {
        [self.navigationController popViewControllerAnimatedWithTransition:UIViewAnimationTransitionFlipFromLeft];
    }
}
- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - goback and uptoserver implement
-(void)Goback:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


//Tell the server which file shared and who can see the shared file
-(void)viewDidAppear:(BOOL)animated{
}

-(void)requestFinished:(NSNumber *)code withMessage:(NSString *)message withData:(id)data{

}

- (void) startWaitCursor {
    BSAppDelegate *appDelegate = (BSAppDelegate*)[[UIApplication sharedApplication] delegate];
	[appDelegate startLoading];
}

- (void) stopWaitCursor {
    BSAppDelegate *appDelegate = (BSAppDelegate*)[[UIApplication sharedApplication] delegate];
	[appDelegate stopLoading];
}
@end
